using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;

namespace TweakSP.Web.Admin
{
    public partial class TweakSP_AdminHierarchy : GlobalAdminPageBase
    {
        protected Table adminHierarchy;

        //TODO: Add SharePoint Object Name to display
        //TODO: Add ECM for each item with "Create PowerShell context script" (to Clipboard, to .ps1, etc)

        private void BuildMenuTemplates()
        {
            #region Site Menu
            MenuTemplate siteMenu = new MenuTemplate();
            siteMenu.ID = "SiteMenu";

            MenuItemTemplate siteSettings = new MenuItemTemplate("Site Settings", "/_layouts/images/actionssettings.gif");
            siteSettings.ClientOnClickNavigateUrl = "%SITE%/_layouts/settings.aspx";
            siteMenu.Controls.Add(siteSettings);

            MenuSeparatorTemplate sepMenu = new MenuSeparatorTemplate();
            siteMenu.Controls.Add(sepMenu);

            MenuItemTemplate poshContext = new MenuItemTemplate("Script Context to PowerShell", "/_layouts/images/ICWSF.GIF");
            poshContext.ClientOnClickScript = "window.open('%SITE%/_layouts/TweakSP_GetPoSH.aspx?REQUESTMODE=Site', 'PoSH', 'width=700,height=500,scrollbars=1');";
            siteMenu.Controls.Add(poshContext);

            this.Controls.Add(siteMenu);

            #endregion Site Menu
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            BuildMenuTemplates();

            SPFarm myFarm = SPFarm.Local;
            SPServerCollection myServers = myFarm.Servers;

            foreach (SPServer myServer in myServers)
            {
                adminHierarchy.Rows.Add(CreateTableRowWithText("&nbsp;"));
                adminHierarchy.Rows.Add(CreateTableRowWithText("Server Name: " + myServer.DisplayName));

                adminHierarchy.Rows.Add(CreateTableRowWithText("&nbsp;Services on this Server: "));
                adminHierarchy.Rows.Add(CreateTableRowWithText("&nbsp;"));
                foreach (SPServiceInstance servinst in myServer.ServiceInstances)
                {
                    adminHierarchy.Rows.Add(CreateTableRowWithText("&nbsp;&nbsp;&nbsp;Service Type: " + servinst.TypeName));
                    if (servinst.Service is SPWebService)
                    {
                        SPWebService serv = (SPWebService)servinst.Service;
                        foreach (SPWebApplication webapp in serv.WebApplications)
                        {
                            adminHierarchy.Rows.Add(CreateTableRowWithText("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Web Application: " + webapp.GetResponseUri(SPUrlZone.Default).AbsoluteUri));

                            foreach (SPSite site in webapp.Sites)
                            {
                                adminHierarchy.Rows.Add(CreateTableRowWithSiteMenu("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site Collection: ", site.Url, site.Url));
                            }
                        }
                    }
                }
            }
        }

        private TableRow CreateTableRowWithText(string text)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Text = text;
            row.Cells.Add(cell);

            return row;
        }

        private TableRow CreateTableRowWithSiteMenu(string text, string siteTitle, string siteUrl)
        {
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            
            Literal lit = new Literal();
            lit.Text = text;

            cell1.Controls.Add(lit);
            cell2.Controls.Add(CreateSiteMenu(siteTitle, siteUrl));

            row.Cells.Add(cell1);
            row.Cells.Add(cell2);

            return row;
        }

        private Microsoft.SharePoint.WebControls.Menu CreateSiteMenu(string siteTitle, string siteUrl)
        {
            Microsoft.SharePoint.WebControls.Menu mnu = new Microsoft.SharePoint.WebControls.Menu();
            mnu.Text = siteTitle;
            mnu.TemplateId = "SiteMenu";
            mnu.TokenValues = "SITE=" + siteUrl;

            return mnu;
        }

    }
}
